package controllers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"testGin/model/mysql"
	"testGin/model/tool"
)

type LogController struct{
	log mysql.Log
}

func (ctr LogController) Index(c *gin.Context) {
	c.HTML(200, "system/log/index.html", gin.H{})
}
func (ctr LogController) GetList(c *gin.Context) {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}

	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	adminId := c.Query("adminId")
	if adminId != "" {
		where = tool.MapWhere("id_admin","=",adminId)
	}
	url := c.DefaultQuery("url", "")
	if url != "" {
		where = tool.MapWhere("url","=",url)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("text","=",name)
	}
	result, err := ctr.log.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}
